package com.soft.javase.day12.bankplus;

/**
 * @author yzh
 * @desc TODO
 * @date 2025/5/19
 */
public class Account {
    private String name;
    private double balance;

    public Account(String name, double balance) {
        this.name = name;
        this.balance = balance;
    }

    public synchronized void deposit(double money) {
        try {
            Thread.sleep(1000);

            balance += money;
            System.out.println(Thread.currentThread().getName() + " 存入 " + money
                    + "，账户余额: " + balance);

            notifyAll();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public synchronized void withdraw(double money) {
        try {
            while (balance < money) {
                System.out.println(Thread.currentThread().getName() + " 想取 " + money
                        + "，但余额不足，等待存款中... (当前余额: " + balance + ")");
                wait();
            }

            Thread.sleep(1000);

            balance -= money;
            System.out.println(Thread.currentThread().getName() + " 取出 " + money
                    + "，账户余额: " + balance);

        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
